package com.haidong.component;

import com.haidong.listener.MyButtonActionListener;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class LoginFrame extends JFrame {
    public LoginFrame(String title) throws Exception {
        //设置窗口属性
        super(title);
        setBounds(400,200,600,400);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        setLayout(null);

        //这部分是登录页面的固定文字
        JLabel upText = new JLabel("欢迎使用单位房产管理系统");
        upText.setFont(new Font("Dialog",0,30));
        JLabel idText = new JLabel("账号:");
        idText.setBounds(150,30,50,40);
        idText.setFont(new Font("Dialog",0,15));
        JLabel pswText = new JLabel("密码:");
        pswText.setBounds(150,100,50,40);
        pswText.setFont(new Font("Dialog",0,15));

        //这部分是添加了文本监听器的账号密码框
        TextField userId = new TextField();
        userId.setBounds(200,30,200,40);
        userId.setFont(new Font("Dialog",0,30));
        TextField userPsw = new TextField();
        userPsw.setBounds(200,100,200,40);
        userPsw.setFont(new Font("Dialog",0,30));

        //这部分是绑定了点击事件的登录按钮
        JButton adminLogin = new JButton("管理员登录");
        adminLogin.setFont(new Font("Dialog",0,12));
        adminLogin.setBounds(180,180,100,50);
        JButton userLogin = new JButton("普通人登录");
        userLogin.setFont(new Font("Dialog",0,12));
        userLogin.setBounds(320,180,100,50);
        //添加按钮监听,按下按钮的同时判别账号密码,正确的话登录
        adminLogin.addActionListener(new MyButtonActionListener(this,userId,userPsw,true));
        userLogin.addActionListener(new MyButtonActionListener(this,userId,userPsw,false));

        //up面板放置上方提示内容,down面板放置下方登录框
        JPanel up = new JPanel();
        up.setBounds(0,0,600,50);
        up.setOpaque(false);
        up.add(upText);

        //登录界面下半部分
        JPanel down = new JPanel(null);
        down.setBounds(0,50,600,350);
        down.setOpaque(false);
        down.add(idText);
        down.add(pswText);
        down.add(userId);
        down.add(userPsw);
        down.add(adminLogin);
        down.add(userLogin);

        //这里写一个最底层的panel将背景层统一,用来放图片(上层容器全设置透明)
        JPanel rootPanel = new JPanel(null);
        rootPanel.setBounds(0,0,600,400);
        rootPanel.setBackground(new Color(25,167,207));
        rootPanel.add(up);
        rootPanel.add(down);

        add(rootPanel);
        setResizable(false);
        setVisible(true);
    }
}
